package com.a2j.web.car.controller;

import com.a2j.beans.base.BaseBean;
import com.a2j.beans.base.BasePageBean;
import com.a2j.beans.base.code.ResponseCode;
import com.a2j.beans.car.sub_brand.dto.CarSubBrandDTO;
import com.a2j.beans.car.sub_brand.dto.CarSubBrandRequestDTO;
import com.a2j.beans.car.sub_brand.vo.CarSubBrandByBrandIdVO;
import com.a2j.beans.car.sub_brand.vo.CarSubBrandRequestVO;
import com.a2j.beans.car.sub_brand.vo.CarSubBrandVO;
import com.a2j.web.car.service.CarSubBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author: bzb
 * Data: 2021/11/18 21:26
 * Desc: 子品牌接口
 */
@RestController
@RequestMapping("/carSubBrand")
@Api(tags = "车子品牌")
public class CarSubBrandController {

    @Resource
    CarSubBrandService service;

    /**
     * 新增子品牌
     * @param vo 子品牌参数
     */
    @PostMapping("/addCarSubBrand")
    @ApiOperation(value = "添加子品牌")
    @ApiImplicitParams({})
    public BaseBean<String> addCarSubBrand(@RequestBody CarSubBrandVO vo) {
        // VO 转 DTO
        val dto = CarSubBrandDTO.builder().build();
        BeanUtils.copyProperties(vo, dto);

        service.addCarSubBrand(dto);
        return BaseBean.success(ResponseCode.SUCCESS);
    }

    /**
     * 查询所有子品牌 (分页)
     * @param record 分页参数
     */
    @GetMapping("/getAllCarSubBrands")
    @ApiOperation(value = "查询所有子品牌 (分页)")
    @ApiImplicitParams({})
    public BaseBean<BasePageBean<CarSubBrandVO>> getAllCarSubBrands(@RequestBody CarSubBrandRequestVO record) {
        val dto = CarSubBrandRequestDTO.builder().build();
        BeanUtils.copyProperties(record, dto);

        return service.getAllCarSubBrands(dto);
    }

    /**
     * 查询所有货源渠道
     */
    @GetMapping("/getAllSourceTypes")
    @ApiOperation(value = "查询所有货源渠道")
    public BaseBean<List<String>> getAllSourceTypes() {
        return service.getAllSourceTypes();
    }

    /**
     * 查询子品牌详情
     * @param id 子品牌id
     */
    @GetMapping("/getCarSubBrandDetail/{id}")
    @ApiOperation(value = "查询子品牌详情")
    @ApiImplicitParam(name = "id", value = "子品牌id", dataType = "int", required = true, defaultValue = "1")
    public BaseBean<CarSubBrandVO> getCarSubBrandDetail(@PathVariable Integer id) {
        return service.getCarSubBrandDetail(id);
    }

    /**
     * 根据品牌id, 查询子品牌集合
     * @param brandId 品牌id
     */
    @GetMapping("/carSubBrandByBrandId/{brandId}")
    @ApiOperation(value = "查询品牌列表, 根据品牌id")
    @ApiImplicitParam(name = "brandId", value = "品牌id", dataType = "int", required = true, defaultValue = "1")
    public BaseBean<List<CarSubBrandByBrandIdVO>> carSubBrandByBrandId(@PathVariable Integer brandId) {
        return service.carSubBrandByBrandId(brandId);
    }

    /**
     * 删除子品牌
     * @param id 子品牌id
     */
    @DeleteMapping("/deleteCarSubBrand")
    @ApiOperation(value = "删除子品牌")
    @ApiImplicitParam(name = "subBrandId", value = "子品牌id", dataType = "int", required = true, defaultValue = "1")
    public BaseBean<String> deleteCarSubBrand(@RequestParam("subBrandId") Integer id) {
        service.deleteCarSubBrand(id);
        return BaseBean.success(ResponseCode.SUCCESS);
    }

    /**
     * 子品牌开关
     * @param id 子品牌id
     * @param enable 开关状态  0:关 1:开
     */
    @PutMapping("/enable")
    @ApiOperation(value = "子品牌开关")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subBrandId", value = "子品牌id", dataType = "int", required = true, defaultValue = "1"),
            @ApiImplicitParam(name = "enable", value = "开关状态  0:关 1:开", dataType = "int", defaultValue = "0")
    })
    public BaseBean<String> enable(@RequestParam("subBrandId") Integer id, Integer enable) {
        if (enable == null) enable = 0;
        service.enable(id, enable);
        return BaseBean.success(ResponseCode.SUCCESS);
    }

    /**
     * 修改子品牌属性
     * @param vo 子品牌属性
     */
    @PutMapping("/updateCarSubBrand")
    @ApiOperation(value = "更新子品牌信息")
    @ApiImplicitParams({})
    public BaseBean<String> updateCarSubBrand(@RequestBody CarSubBrandVO vo) {
        // VO 转 DTO
        val dto = CarSubBrandDTO.builder().build();
        BeanUtils.copyProperties(vo, dto);

        service.updateCarSubBrand(dto);
        return BaseBean.success(ResponseCode.SUCCESS);
    }

}
